/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.dao;

import java.io.FileInputStream;
import org.dbunit.DBTestCase;
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.CompositeOperation;
import org.dbunit.operation.DatabaseOperation;

/**
 *
 * Test unitaire de l'acces a la base ecom. Tous les tests
 * unitaires de DBUnit devront hÈriter de cette classe au lieu
 * d'heriter de DBTestCase.
 *
 * @author simonrichard
 */
public class EcomTestCase extends DBTestCase {

    /**
     * system properties initializing constructor.
     */
    public EcomTestCase() {
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver");
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:mysql://localhost:3306/ecom");
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "root");
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "root");
    }

    /** Dataset representant la base de donnees initiale.
     * Cette methode est abstraite dans DBTestCase et peut etre implementee
     * de la meme facon pour tous les tests, d'ou sa place ici.
     */
    protected IDataSet getDataSet() throws Exception {
        return new FlatXmlDataSet(new FileInputStream("src/conf/dbunit/database_schema.xml"));
    }

//    @Override
    /** Indispensable pour obtenir une connexion */
//    protected IDatabaseTester newDatabaseTester() throws Exception {
//        return new JdbcDatabaseTester("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/ecom",
//                "root", "root");
//    }

    @Override
    /** Operation sur la base de donnees executee avant tout test */
    protected DatabaseOperation getSetUpOperation() throws Exception {
        DatabaseOperation result = new CompositeOperation( new DatabaseOperation [] {
            DatabaseOperation.TRUNCATE_TABLE,
            DatabaseOperation.INSERT
        });

        System.out.println("Remise à zero de la base");

        return result;
    }
}
